<template>
  <el-form
    ref="userForm"
    :model="userInfo"
    label-width="120px"
    style="margin-left: 120px; margin-top: 30px"
  >
    <el-form-item label="姓名:">
      <el-input v-model="userInfo.username" style="width:300px" />
    </el-form-item>
    <el-form-item label="密码:">
      <el-input v-model="userInfo.password" type="password" style="width:300px" />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click.native="hSubmit">更新</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { getUserInfoById, updateUserInfo } from '@/api/employees'
export default {
  name: 'LoginSetting',
  data() {
    return {
      userInfo: {
        username: '',
        password: ''
      }
    }
  },
  created() {
    this.loadUserInfoById()
  },
  methods: {
    // 通过id获取个人信息
    async loadUserInfoById() {
      const res = await getUserInfoById(this.$route.params.id)
      console.log(res)
      // 表单回填
      this.userInfo = res.data
    },
    // 用户点击更新
    hSubmit() {
      // 1.表单校验
      this.$refs.userForm.validate(async(valid) => {
        if (valid) {
          // 2.调用接口 更新用户信息
          const res = await updateUserInfo(this.userInfo)
          console.log(res)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
